<?php

namespace Console\Logic;

use \Common\Logic\BaseLogic;

/**
 * 信鸽控制器
 */
class PigeonLogic extends BaseLogic {

    private $sexDic = array(
        "1" => "雄",
        "2" => "雌",
        "3" => "未知"
    );
    private $salesStatusDic = array(
        "-1" => "未上架",
        "1" => "展售中",
        "2" => "已出售",
        "2" => "已死亡",
    );
    private $pigeonModel;

    public function __construct() {
        parent::__construct();
        $this->pigeonModel = D('Pigeon');
    }

    /**
     * 获取信鸽列表
     * @param type $page
     * @param type $size
     * @param type $where
     * @param type $order
     * @return type
     */
    public function lists($page = 1, $size = 20, $where = array(), $order = array()) {
        $where["state"] = 1;
        return $this->pigeonModel->where($where)->page($page, $size)->order($order)->select();
    }

    /**
     * 获取数据条数
     * @param type $where
     * @return type
     */
    public function total($where = array()) {
        $where["state"] = 1;
        return $this->pigeonModel->where($where)->count();
    }

    /**
     * 查找一条数据
     * @param array $where
     * @return type
     */
    public function find($where = array()) {
        $where["state"] = 1;
        return $this->pigeonModel->where($where)->find();
    }

    /**
     * 获取性别字典
     * @return type
     */
    public function getSexDic() {
        return $this->sexDic;
    }

    /**
     * 销售状态字典
     * @return type
     */
    public function getSalesStatusDic() {
        return $this->salesStatusDic;
    }

    /**
     * 添加信鸽
     * @param type $userId
     * @param type $data
     * @return type
     */
    public function addPigeon($userId, $data) {
        if (empty($userId)) {
            return static::$res->err("添加人不能为空");
        }

        $fields = array(
            "cid" => array("empty" => "环号不能为空"),
            "name" => array(),
            "sex" => array(),
            "color" => array(),
            "eye" => array(),
            "bloodlines" => array(),
            "raiser" => array(),
            "birthday" => array(),
            "sales_status" => array(),
            "price" => array(),
            "client_info" => array(),
            "father_id" => array(),
            "mother_id" => array(),
            "bloodlines_cont" => array(),
            "remark" => array(),
            "body_img" => array(),
            "eye_img" => array(),
            "bloodlines_img" => array(),
            "pr_type" => array(),
        );

        $verifyRes = $this->verifyData($fields, $data);
        if (static::$res->isErr($verifyRes)) {
            return $verifyRes;
        }
        $data = $verifyRes['Result'];

        //判断环号是否已经被占用
        $where = array(
            "cid" => $data["cid"]
        );
        if (!empty($this->total($where))) {
            return static::$res->err("环号已经存在");
        }

        $data['sex'] = $data['sex'] ? $data['sex'] : 3;
        $data["price"] = $data["price"] * 100;
        $data["pr_type"] = $data["pr_type"] ? 1 : 0;


        //身体图片压缩
        if (!empty($data['body_img'])) {
            $url = $data['body_img'];

            if (strrpos($url, '.')) {
                $imgType = substr($url, strrpos($url, '.') + 1);
            } else {
                $imgType = 'jpeg';
            }

            $miniImgUrl = "/pigeonSys/ImgFile/" . date("Ymd") . "_mini/" . time() . "_" . rand(1111, 9999) . '.' . $imgType;

            $miniFileName = WEB_PATH . "/.." . $miniImgUrl;
            $fileName = WEB_PATH . "/.." . $url;

            //等比例压缩，高度固定
            if (thumb($fileName, $miniFileName, array("h" => 165))) {
                $data['body_mini_img'] = $miniImgUrl;
            }
        } else {
            $data['body_mini_img'] = "/pigeonSys/ImgTemp/pigeon_body_def.jpg";
        }

        //眼睛图片压缩
        if (!empty($data['eye_img'])) {
            $url = $data['eye_img'];

            if (strrpos($url, '.')) {
                $imgType = substr($url, strrpos($url, '.') + 1);
            } else {
                $imgType = 'jpeg';
            }

            $miniImgUrl = "/pigeonSys/ImgFile/" . date("Ymd") . "_mini/" . time() . "_" . rand(1111, 9999) . '.' . $imgType;

            $miniFileName = WEB_PATH . "/.." . $miniImgUrl;
            $fileName = WEB_PATH . "/.." . $url;

            //限定死宽高
            if (thumb($fileName, $miniFileName, array("w" => 95, "h" => 95))) {
                $data['eye_mini_img'] = $miniImgUrl;
            }
        } else {
            $data['eye_mini_img'] = "/pigeonSys/ImgTemp/pigeon_eye_def.jpg";
        }

        //拼接身体和眼睛图片
        $BKFileName = WEB_PATH . "/ImgTemp/body_eye_bk.jpg";
        $BEMiniImgUrl = "/pigeonSys/ImgFile/" . date("Ymd") . "_mini/" . time() . "_" . rand(1111, 9999) . '.jpg';
        $BEMiniFileName = WEB_PATH . "/.." . $BEMiniImgUrl;

        //拼接右下角身体图片
        $BMiniImgUrl = $data['body_mini_img'];
        $BMiniFileName = WEB_PATH . "/.." . $BMiniImgUrl;
        water($BKFileName, $BMiniFileName, $BEMiniFileName, array("place" => \Think\Image::IMAGE_WATER_SOUTHEAST));

        //拼接左上角眼睛图片
        $EMiniImgUrl = $data['eye_mini_img'];
        $EMiniFileName = WEB_PATH . "/.." . $EMiniImgUrl;
        water($BEMiniFileName, $EMiniFileName, $BEMiniFileName);

        $data['body_eye_mini_img'] = $BEMiniImgUrl;
        $data['state'] = 1;
        $data['add_time'] = date("Y-m-d H:i:s");
        $data['add_user'] = $userId;

        $id = $this->pigeonModel->add($data);

        if (empty($id)) {
            return static::$res->err("数据库异常");
        }

        return static::$res->suc("成功", $id);
    }

    /**
     * 编辑信鸽
     * @param type $userId
     * @param type $pigeonId
     * @param type $data
     * @return type
     */
    public function editPigeon($userId, $pigeonId, $data) {
        if (empty($userId)) {
            return static::$res->err("编辑人不能为空");
        }

        if (empty($pigeonId)) {
            return static::$res->err("信鸽ID不能为空");
        }

        $fields = array(
            "cid" => array("empty" => "环号不能为空"),
            "name" => array(),
            "sex" => array(),
            "color" => array(),
            "eye" => array(),
            "bloodlines" => array(),
            "raiser" => array(),
            "birthday" => array(),
            "sales_status" => array(),
            "price" => array(),
            "client_info" => array(),
            "father_id" => array(),
            "mother_id" => array(),
            "bloodlines_cont" => array(),
            "remark" => array(),
            "body_img" => array(),
            "eye_img" => array(),
            "bloodlines_img" => array(),
            "pr_type" => array(),
        );

        $verifyRes = $this->verifyData($fields, $data);
        if (static::$res->isErr($verifyRes)) {
            return $verifyRes;
        }
        $data = $verifyRes['Result'];


        //判断环号是否已经被占用
        $where = array(
            "cid" => $data["cid"],
            "id" => array("neq", $pigeonId)
        );
        if (!empty($this->total($where))) {
            return static::$res->err("环号已经存在");
        }

        $data['sex'] = $data['sex'] ? $data['sex'] : 3;
        $data["price"] = $data["price"] * 100;
        $data["pr_type"] = $data["pr_type"] ? 1 : 0;


        //身体图片压缩
        if (!empty($data['body_img'])) {
            $url = $data['body_img'];

            if (strrpos($url, '.')) {
                $imgType = substr($url, strrpos($url, '.') + 1);
            } else {
                $imgType = 'jpeg';
            }

            $miniImgUrl = "/pigeonSys/ImgFile/" . date("Ymd") . "_mini/" . time() . "_" . rand(1111, 9999) . '.' . $imgType;

            $miniFileName = WEB_PATH . "/.." . $miniImgUrl;
            $fileName = WEB_PATH . "/.." . $url;

            //等比例压缩，高度固定
            if (thumb($fileName, $miniFileName, array("h" => 160))) {
                $data['body_mini_img'] = $miniImgUrl;
            }
        } else {
            $data['body_mini_img'] = "";
        }

        //眼睛图片压缩
        if (!empty($data['eye_img'])) {
            $url = $data['eye_img'];

            if (strrpos($url, '.')) {
                $imgType = substr($url, strrpos($url, '.') + 1);
            } else {
                $imgType = 'jpeg';
            }

            $miniImgUrl = "/pigeonSys/ImgFile/" . date("Ymd") . "_mini/" . time() . "_" . rand(1111, 9999) . '.' . $imgType;

            $miniFileName = WEB_PATH . "/.." . $miniImgUrl;
            $fileName = WEB_PATH . "/.." . $url;

            //限定死宽高
            if (thumb($fileName, $miniFileName, array("w" => 95, "h" => 95))) {
                $data['eye_mini_img'] = $miniImgUrl;
            }
        } else {
            $data['eye_mini_img'] = "";
        }

        //拼接身体和眼睛图片
        if (!empty($data['body_mini_img']) || !empty($data['eye_mini_img'])) {
            $BKFileName = WEB_PATH . "/ImgTemp/body_eye_bk.jpg";
            $BEMiniImgUrl = "/pigeonSys/ImgFile/" . date("Ymd") . "_mini/" . time() . "_" . rand(1111, 9999) . '.jpg';
            $BEMiniFileName = WEB_PATH . "/.." . $BEMiniImgUrl;

            //进行身体拼接
            $BMiniImgUrl = $data['body_mini_img'];
            if ($BMiniImgUrl) {
                $BMiniFileName = WEB_PATH . "/.." . $BMiniImgUrl;

                $place = \Think\Image::IMAGE_WATER_SOUTHEAST;
                if (empty($data['eye_mini_img'])) {
                    $place = \Think\Image::IMAGE_WATER_CENTER;
                }

                water($BKFileName, $BMiniFileName, $BEMiniFileName, array("place" => $place));
            }

            //进行眼睛拼接
            $EMiniImgUrl = $data['eye_mini_img'];
            if ($EMiniImgUrl) {
                $EMiniFileName = WEB_PATH . "/.." . $EMiniImgUrl;

                $place = \Think\Image::IMAGE_WATER_NORTHWEST;
                if (empty($data['body_mini_img'])) {
                    $place = \Think\Image::IMAGE_WATER_CENTER;
                }

                if (empty($data['body_mini_img'])) {
                    water($BKFileName, $EMiniFileName, $BEMiniFileName, array("place" => $place));
                } else {
                    water($BEMiniFileName, $EMiniFileName, $BEMiniFileName, array("place" => $place));
                }
            }

            $data['body_eye_mini_img'] = $BEMiniImgUrl;
        } else {
            $data['body_eye_mini_img'] = "";
        }


        $data['edit_time'] = date("Y-m-d H:i:s");
        $data['edit_user'] = $userId;

        $infoWhere = array(
            "id" => $pigeonId
        );
        if (empty($this->total($infoWhere))) {
            return static::$res->err("无法找到信鸽数据");
        }

        $count = $this->pigeonModel->where($infoWhere)->save($data);

        if (empty($count)) {
            return static::$res->err("数据库异常");
        }

        return static::$res->suc("成功");
    }

    /**
     * 删除信鸽
     * @param type $userId
     * @param type $pigeonId
     * @return type
     */
    public function delPigeon($userId, $pigeonId) {
        if (empty($userId)) {
            return static::$res->err("删除人不能为空");
        }

        if (empty($pigeonId)) {
            return static::$res->err("信鸽ID不能为空");
        }

        $infoWhere = array(
            "id" => $pigeonId
        );

        $data = array(
            "state" => -1,
            "edit_time" => date("Y-m-d H:i:s"),
            "edit_user" => $userId
        );

        $count = $this->pigeonModel->where($infoWhere)->save($data);
        if (empty($count)) {
            return static::$res->err("数据库异常");
        }

        return static::$res->suc("成功");
    }

}
